﻿using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.U2D;
using UnityEngine.UI;

public class ChoseServerPanel : BasePanel
{
    //左右滚动窗口
    public ScrollRect svleft;
    public ScrollRect svright;

    //上一次登录的区
    public TextMeshProUGUI preServerTxt;
    public Image preServerState;

    //当前选服区域
    public TextMeshProUGUI regionTxt;

    //存储左侧选项物件
    public List<GameObject> leftItemList;
    //存储右侧选项物件
    public List<GameObject> rightItemList;

    protected override void Init()
    {
        leftItemList = new List<GameObject>();
        rightItemList = new List<GameObject>();
    }

    public override void ShowMe()
    {
        base.ShowMe();

        if (LoginManager.Instance.LoginData.preServerID < 0)
        {
            preServerTxt.text = "无上次选择的服务器";
            preServerState.color = new Color(1.0f, 1.0f, 1.0f, 0.0f);
        }
        else
        {
            //生成上次登录服务器
            ServerData sd = LoginManager.Instance.ServerData[LoginManager.Instance.LoginData.preServerID];
            preServerTxt.text = sd.id + "区 " + sd.name;
            preServerState.color = new Color(1.0f, 1.0f, 1.0f, 1.0f);
            ChangeState(sd.state);         
        }

        //生成左侧区域选项  以5个服务器为分界
        int num = (LoginManager.Instance.ServerData.Count / 5) +
            (LoginManager.Instance.ServerData.Count % 5 ==0 ? 0 : 1);

        for (int i = 0; i < num; i++)
        {
            int begin = i * 5 + 1;
            int end = Mathf.Clamp((i + 1) * 5,1,LoginManager.Instance.ServerData.Count);

            GameObject obj = Instantiate(Resources.Load<GameObject>("UI/ServerLeftItem"));
            obj.GetComponent<ServerLeftItem>().Initialize(begin, end);
            obj.transform.SetParent(svleft.content, false);
            leftItemList.Add(obj);
        }

        //根据上次的选择，生成对应面板
        if (LoginManager.Instance.LoginData.preServerID < 0)
        {
            int tb = 1;
            int te = Mathf.Min(5,LoginManager.Instance.ServerData.Count);
            UpdateServer(tb, te);
        }
        else
        {
            int t = LoginManager.Instance.LoginData.preServerID / 5;
            int tb = t * 5 + 1;
            int te = Mathf.Clamp((t + 1) * 5, 1, LoginManager.Instance.ServerData.Count);
            UpdateServer(tb, te);
        }

        regionTxt.text = "";
    }

    //由左侧调用
    public void UpdateServer(int begin,int end)
    {
        regionTxt.text = "服务器" + begin + " - " + end;

        foreach (GameObject t in rightItemList)
        {
            Destroy(t);
        }

        rightItemList.Clear();

        //生成右侧具体的服务器
        for (int i = begin; i <= end; i++)
        {
            GameObject obj = Instantiate(Resources.Load<GameObject>("UI/ServerRightItem"));
            obj.GetComponent<ServerRightItem>().Initialize(i - 1);
            obj.transform.SetParent(svright.content, false);
            rightItemList.Add(obj);
        }
    }

    private void ChangeState(int state)
    {
        SpriteAtlas sas = Resources.Load<SpriteAtlas>("UIAtlas");
        switch (state)
        {
            case 0:
            case 1:
                preServerState.sprite = sas.GetSprite("ui_DL_liuchang_01"); //流畅
                break;
            case 2:
                preServerState.sprite = sas.GetSprite("ui_DL_fanhua_01");  //繁忙
                break;
            case 3:
                preServerState.sprite = sas.GetSprite("ui_DL_huobao_01");  //火爆
                break;
            case 4:
                preServerState.sprite = sas.GetSprite("ui_DL_weihu_01"); //维护
                break;
        }
    }
}
